package top.songxs.yygh.hosp.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import top.songxs.yygh.common.exp.DiyException;
import top.songxs.yygh.common.utils.MD5;
import top.songxs.yygh.hosp.service.HospitalSetService;
import top.songxs.yygh.model.hosp.HospitalSet;

import java.util.Map;
@Component
public class AckUtil {
    @Autowired
    HospitalSetService hospitalSetService;

    public void AckSign(Map<String, String> map) {

        String hoscode = map.get("hoscode");
        if (StringUtils.isEmpty(hoscode)) {
            throw new DiyException(20001, "医院编号为空！");
        }
        //如果查不出来  就是hospitalSet里面没有  就代表没有开通医院权限
        HospitalSet hospitalSet = hospitalSetService.getByHosCode(hoscode);
        if (hospitalSet == null) {
            throw new DiyException(20001, "未开通医院权限！");
        }
        //如果hospitalSet不是1的话 就代表没有开通医院权限 抛出自定义异常
        if (hospitalSet.getStatus() != 1) {
            throw new DiyException(20001, "未开通医院权限！");
        }
        //2 判断医院传过来的签名是否正确
        //这个签名是加密的 需要把数据库中的签名拿出来加密后比对
        String sign = map.get("sign");
        String encrypt = MD5.encrypt(hospitalSet.getSignKey());
        if (!encrypt.equalsIgnoreCase(sign)) {
            throw new DiyException(20001, "签名校验未通过！");
        }


    }
}
